package com.lcm.openapi.resolver;

import com.lcm.openapi.common.exception.ServiceException;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URI;
import java.net.URL;
import java.nio.charset.StandardCharsets;

public class URIResolver implements DataSourceResolver{

    @Override
    public Reader getReader(String source) {
        try {
            URI uri = new URI(source);
            URL url = uri.toURL();
            InputStream in = url.openStream();
            return new InputStreamReader(in, StandardCharsets.UTF_8);
        } catch (Exception e) {
            throw new ServiceException("资源读取失败");
        }
    }
}
